package com.longxiao.service.util;


import com.longxiao.vo.TaskVo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DateUtil {
	
	public static final String FORMAT_TIME = "yyyy-MM-dd HH:mm:ss";
	
	private static final String[] WEEK_DAY = {
		"一", "二", "三", "四", "五", "六", "七"
	};


	private static final String[] HOURS = {
			"8:00", "8:30", "9:00", "9:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30"
	};

	private static final String[] DAYS_CN = {
			"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"
	};

	private static final String[] DAYS_EN = {
			"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
	};

	/**
	 * 获取本周的开始时间
	 * @return
	 */
	public static Date getWeekStartTime() {
		Calendar currentDate = new GregorianCalendar();   
		currentDate.setFirstDayOfWeek(Calendar.MONDAY);  
		currentDate.set(Calendar.HOUR_OF_DAY, 0);  
		currentDate.set(Calendar.MINUTE, 0);  
		currentDate.set(Calendar.SECOND, 0);  
		currentDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);  
		return currentDate.getTime();
	}
	
	/**
	 * 获取本周的结束时间
	 * @return
	 */
	public static Date getWeekEndTime() {
		Calendar currentDate = new GregorianCalendar();   
		currentDate.setFirstDayOfWeek(Calendar.MONDAY);  
		currentDate.set(Calendar.HOUR_OF_DAY, 23);  
		currentDate.set(Calendar.MINUTE, 59);  
		currentDate.set(Calendar.SECOND, 59);  
		currentDate.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);  
		return currentDate.getTime();
	}

	
	/**
	 * 生成周日期列表
	 * @return
	 */
	public static List<TaskVo> getWeekDayList(Date weekStartTime, Date weekEndTime) {
		List<TaskVo> dayList = new ArrayList<TaskVo>();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		Date time = weekStartTime;
		cal.setTime(time);
		int tmp = 0;
		while ((time.after(weekStartTime) || time.equals(weekStartTime)) && time.before(weekEndTime)) {
			TaskVo task = new TaskVo(WEEK_DAY[tmp++], sdf.format(time), 0);
			dayList.add(task);
			// 日期向后偏移
			cal.add(Calendar.DAY_OF_MONTH, 1);
			time = cal.getTime();
		}
		return dayList;
	}
	
	/**
	 * 获得日期在周内的索引
	 * @param time
	 * @return
	 */
	public static int getWeekDayIndex(Date time) {
		Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w == 0)
            w = 7;
        return w;
	}
	
	
	/**
	 * 日期格式化
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static String format(Date date, String formatStr) {
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		String result = format.format(date);
		return result;
	}



	/**
	 * 指定输入日期为当年的第几周
	 * @param date
	 * @return
     */
	public static int weekOfYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		int week = calendar.get(Calendar.WEEK_OF_YEAR);
		return week;
	}


	public static Date parse(String dateStr, String formatStr) {
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

//	/**
//	 * 学生排期表
//	 * @return
//     */
//	public static Cell[][] stuSchedule() {
//		Cell[][] cell = new Cell[7][HOURS.length + 1];
//		// 1. 绘制表头
//		int line = 0;
//		StringBuilder builder = new StringBuilder();
//		for (int col = 0; col < HOURS.length; col ++) {
//			builder.append("0")
//					.append(col);
//			cell[0][col + 1] = new Cell(
//					builder.toString(), HOURS[col], null, Boolean.TRUE, Boolean.FALSE, Boolean.FALSE, 0
//			);
//			builder.delete(0, builder.length());
//		}
//		line ++;
//		// 2. 开始绘制表格
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		Calendar nextWeekCursor = Calendar.getInstance();
//		nextWeekCursor.setTime(getNextWeekMon(new Date()));
//
//		// 行循环
//		for (int table_line = 0; table_line < DAYS_CN.length; table_line ++) {
//			// 列循环
//			// int id, String intro, String date, boolean isTitle, boolean isSchedule, boolean isSucc, int rewardcp
//			String date = sdf.format(nextWeekCursor.getTime());
//			builder.append(line).append("0");
//			cell[table_line][0] = new Cell(
//					builder.toString(), DAYS_CN[table_line], date, Boolean.TRUE, Boolean.FALSE, Boolean.FALSE, 0
//			);
//			builder.delete(0, builder.length());
//			for (int col = 1; col < HOURS.length; col ++) {
//				builder.append(line).append(col);
//				cell[table_line][col] = new Cell(
//						builder.toString(), "", date, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, 0
//				);
//				builder.delete(0, builder.length());
//			}
//			// 游标向下一天
//			nextWeekCursor.add(Calendar.DAY_OF_YEAR, 1);
//			line ++;
//		}
//		return cell;
//	}

	/**
	 * 获取指定时间的下周一的日期
	 * @return
     */
	public static Date getNextWeekMon(Date curr) {
		int n = getWeekDayIndex(curr);
		int tmp = 7 - n + 1;
		Calendar cal = Calendar.getInstance();
		cal.setTime(curr);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.add(Calendar.DAY_OF_YEAR, tmp);
		return cal.getTime();
	}

	/**
	 * 获取指定时间下下周一的日期
	 * @param curr
	 * @return
     */
	public static Date getNext2WeekMon(Date curr) {
		int n = getWeekDayIndex(curr);
		int tmp = 14 - n + 1;
		Calendar cal = Calendar.getInstance();
		cal.setTime(curr);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.add(Calendar.DAY_OF_YEAR, tmp);
		return cal.getTime();
	}

	/**
	 * 获取指定时间的下周日的日期
	 * @param curr
	 * @return
     */
	public static Date getNextWeekSun(Date curr) {
		int n = getWeekDayIndex(curr);
		int tmp = 14 - n;
		Calendar cal = Calendar.getInstance();
		cal.setTime(curr);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.add(Calendar.DAY_OF_YEAR, tmp);
		return cal.getTime();
	}

	/**
	 * 获取下下周日的日期
	 * @param curr
	 * @return
     */
	public static Date getNext2WeekSun(Date curr) {
		int n = getWeekDayIndex(curr);
		int tmp = 21 - n;
		Calendar cal = Calendar.getInstance();
		cal.setTime(curr);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.add(Calendar.DAY_OF_YEAR, tmp);
		return cal.getTime();
	}

	public static Date getNextMinutes(Date curr,int minutes) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(curr);
		cal.add(Calendar.MINUTE, minutes);
		return cal.getTime();
	}
	
	public static void main(String[] args) {
//		Calendar cal = Calendar.getInstance();
//		cal.add(Calendar.DAY_OF_YEAR, 3);
//		System.out.println(format(cal.getTime(), FORMAT_TIME));
//		Date time = getNextWeekSun(new Date());
//		System.out.println(format(time, FORMAT_TIME));
		Date date = getNext2WeekSun(new Date());
		System.out.println(date);
	}

	public static Date gmtTimeToLocalTime(String gmtTime) {
		return new Date(gmtTime);
	}

//	public static void main(String[] args) {
//		Calendar cal = Calendar.getInstance();
//		cal.add(Calendar.DAY_OF_YEAR, 5);
//		System.out.println(DateUtil.format(cal.getTime(), "yyyy-MM-dd"));
//		System.out.println(weekOfYear(cal.getTime()));
//	}


	/**
	 * 获取本周的剩余秒数
	 * @return
     */
	public static int getWeekSurplusSeconds() {
		Date weekEndTime = DateUtil.getWeekEndTime();
		Date currTime = new Date();
		long mills = weekEndTime.getTime() - currTime.getTime();
		int result = (int) mills / 1000;
		return result;
	}

	/*
	*获取今天零点时间
	*/
	public static String getTodayZero(){
		Date now = new Date();
		return format(now, "yyyy-MM-dd") +" 00:00:00";
	}







}
